{
 "cells": [
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple\r\n",
      "Requirement already satisfied: volcengine-python-sdk[ark] in /Users/ranhongliang/Documents/workspace/langchain_docs/.venv/lib/python3.10/site-packages (1.0.93)\r\n",
      "Requirement already satisfied: certifi>=2017.4.17 in /Users/ranhongliang/Documents/workspace/langchain_docs/.venv/lib/python3.10/site-packages (from volcengine-python-sdk[ark]) (2024.7.4)\r\n",
      "Requirement already satisfied: python-dateutil>=2.1 in /Users/ranhongliang/Documents/workspace/langchain_docs/.venv/lib/python3.10/site-packages (from volcengine-python-sdk[ark]) (2.9.0.post0)\r\n",
      "Requirement already satisfied: six>=1.10 in /Users/ranhongliang/Documents/workspace/langchain_docs/.venv/lib/python3.10/site-packages (from volcengine-python-sdk[ark]) (1.16.0)\r\n",
      "Requirement already satisfied: urllib3>=1.23 in /Users/ranhongliang/Documents/workspace/langchain_docs/.venv/lib/python3.10/site-packages (from volcengine-python-sdk[ark]) (2.2.2)\r\n",
      "Requirement already satisfied: pydantic<3,>=1.9.0 in /Users/ranhongliang/Documents/workspace/langchain_docs/.venv/lib/python3.10/site-packages (from volcengine-python-sdk[ark]) (2.8.2)\r\n",
      "Requirement already satisfied: httpx<1,>=0.23.0 in /Users/ranhongliang/Documents/workspace/langchain_docs/.venv/lib/python3.10/site-packages (from volcengine-python-sdk[ark]) (0.27.0)\r\n",
      "Requirement already satisfied: anyio<5,>=3.5.0 in /Users/ranhongliang/Documents/workspace/langchain_docs/.venv/lib/python3.10/site-packages (from volcengine-python-sdk[ark]) (4.4.0)\r\n",
      "Requirement already satisfied: idna>=2.8 in /Users/ranhongliang/Documents/workspace/langchain_docs/.venv/lib/python3.10/site-packages (from anyio<5,>=3.5.0->volcengine-python-sdk[ark]) (3.7)\r\n",
      "Requirement already satisfied: sniffio>=1.1 in /Users/ranhongliang/Documents/workspace/langchain_docs/.venv/lib/python3.10/site-packages (from anyio<5,>=3.5.0->volcengine-python-sdk[ark]) (1.3.1)\r\n",
      "Requirement already satisfied: exceptiongroup>=1.0.2 in /Users/ranhongliang/Documents/workspace/langchain_docs/.venv/lib/python3.10/site-packages (from anyio<5,>=3.5.0->volcengine-python-sdk[ark]) (1.2.2)\r\n",
      "Requirement already satisfied: typing-extensions>=4.1 in /Users/ranhongliang/Documents/workspace/langchain_docs/.venv/lib/python3.10/site-packages (from anyio<5,>=3.5.0->volcengine-python-sdk[ark]) (4.12.2)\r\n",
      "Requirement already satisfied: httpcore==1.* in /Users/ranhongliang/Documents/workspace/langchain_docs/.venv/lib/python3.10/site-packages (from httpx<1,>=0.23.0->volcengine-python-sdk[ark]) (1.0.5)\r\n",
      "Requirement already satisfied: h11<0.15,>=0.13 in /Users/ranhongliang/Documents/workspace/langchain_docs/.venv/lib/python3.10/site-packages (from httpcore==1.*->httpx<1,>=0.23.0->volcengine-python-sdk[ark]) (0.14.0)\r\n",
      "Requirement already satisfied: annotated-types>=0.4.0 in /Users/ranhongliang/Documents/workspace/langchain_docs/.venv/lib/python3.10/site-packages (from pydantic<3,>=1.9.0->volcengine-python-sdk[ark]) (0.7.0)\r\n",
      "Requirement already satisfied: pydantic-core==2.20.1 in /Users/ranhongliang/Documents/workspace/langchain_docs/.venv/lib/python3.10/site-packages (from pydantic<3,>=1.9.0->volcengine-python-sdk[ark]) (2.20.1)\r\n"
     ]
    }
   ],
   "source": [
    "! pip install --upgrade 'volcengine-python-sdk[ark]'"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-22T04:10:14.471451Z",
     "start_time": "2024-07-22T04:10:13.433521Z"
    }
   },
   "id": "b782375cd7f4784",
   "execution_count": 5
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "----- standard request -----\n",
      "十字花科植物约有 375 属，3200 种，广布于全世界温带、亚热带地区，尤以热带和亚热带地区为多。中国有 95 属、425 种、124 变种和 9 个变型，全国各地均有分布，以西南、西北、东北高山区及丘陵地带为多，平原及沿海地区较少。常见的十字花科植物有以下几种：\n",
      "1. 白菜：白菜是十字花科芸薹属植物，二年生草本，高 40-60 厘米，常全株无毛，有时叶下面中脉上有少数刺毛。原产于中国北方，是中国的传统蔬菜。\n",
      "2. 萝卜：萝卜是十字花科萝卜属二年或一年生草本植物，高可达 100 厘米，直根肉质，长圆形、球形或圆锥形，外皮绿色、白色或红色。中国各地普遍栽培。\n",
      "3. 甘蓝：甘蓝是十字花科芸薹属植物，二年生草本，被粉霜。矮且粗壮一年生茎肉质，不分枝，绿色或灰绿色。原产于地中海沿岸，现中国各地普遍栽培。\n",
      "4. 花椰菜：花椰菜是十字花科芸薹属植物，二年生草本，高 60-90 厘米，被粉霜。茎直立，有分枝。起源于地中海东部海岸，约在 19 世纪初清光绪年间引进中国。\n",
      "5. 芥菜：芥菜是十字花科芸薹属一年生草本植物，高可达 150 厘米，幼茎及叶具刺毛，有辣味；茎直立，叶片柄具小裂片；茎下部叶较小，边缘有缺刻或锯齿，茎上部叶窄披针形，边缘具不明显疏齿或全缘。\n",
      "\n",
      "这只是十字花科植物中的一部分，还有许多其他的植物也属于这个科。如果你需要了解更多关于十字花科植物的信息，可以继续向我提问。\n",
      "----- streaming request -----\n",
      "十字花科植物是一个拥有广泛种类的植物科，包括许多我们日常生活中常见的蔬菜和花卉。以下是一些常见的十字花科植物：\n",
      "- **西兰花**：十字花科芸薹属植物，二年生草本，高可达 100 厘米。茎直立，粗壮，无毛，基生叶及下部叶长圆形，顶端圆形，边缘有不明显小齿或有柄具宽翅的叶柄，上部叶小，无柄，全缘。\n",
      "- **花椰菜**：十字花科芸薹属植物，二年生草本，高 60-90 厘米，被粉霜。茎直立，粗壮，有分枝。基生叶及下部叶长圆形至椭圆形，长 2-3 次羽状分裂，上部叶小，长圆形，抱茎，全缘或具细牙齿。\n",
      "- **白菜**：十字花科芸薹属植物，二年生草本，高 40-60 厘米，常全株无毛，有时叶下面中脉上有少数刺毛。基生叶多数，大形，倒卵状长圆形至宽倒卵形，长 30-60 厘米，宽不及长的一半，顶端圆钝，边缘皱缩，波状，具不显明牙齿。\n",
      "- **萝卜**：十字花科萝卜属二年或一年生草本植物，直根肉质，长圆形、球形或圆锥形，外皮绿色、白色或红色。基生叶片和下部茎生叶大头羽状半裂，长 8-30 厘米，宽 3-5 厘米，顶端裂片卵形，侧裂片 4-6 对，长圆形，有钝齿，疏生粗毛，总状花序顶生及腋生。\n",
      "- **芥菜**：十字花科芸薹属一年生草本植物，高可达 150 厘米，幼茎及叶具刺毛，有辣味。茎直立，叶片柄具小裂片。茎下部叶较小，边缘有缺刻或锯齿，茎上部叶窄披针形，边缘具不明显疏齿或全缘。\n",
      "\n",
      "以上只是十字花科植物的一部分，还有许多其他的蔬菜和花卉也属于这个科。\n"
     ]
    }
   ],
   "source": [
    "from volcenginesdkarkruntime import Ark\n",
    "from dotenv import load_dotenv\n",
    "import os\n",
    "\n",
    "load_dotenv()\n",
    "\n",
    "os.environ[\"ARK_API_KEY\"] = os.getenv(\"ARK_API_KEY\")\n",
    "\n",
    "\n",
    "client = Ark(\n",
    "    base_url=\"https://ark.cn-beijing.volces.com/api/v3\",\n",
    ")\n",
    "\n",
    "# Non-streaming:\n",
    "print(\"----- standard request -----\")\n",
    "completion = client.chat.completions.create(\n",
    "    model=\"ep-20240722023613-lhstw\",\n",
    "    messages = [\n",
    "        {\"role\": \"system\", \"content\": \"你是豆包，是由字节跳动开发的 AI 人工智能助手\"},\n",
    "        {\"role\": \"user\", \"content\": \"常见的十字花科植物有哪些？\"},\n",
    "    ],\n",
    ")\n",
    "print(completion.choices[0].message.content)\n",
    "\n",
    "# Streaming:\n",
    "print(\"----- streaming request -----\")\n",
    "stream = client.chat.completions.create(\n",
    "    model=\"ep-20240722023613-lhstw\",\n",
    "    messages = [\n",
    "        {\"role\": \"system\", \"content\": \"你是豆包，是由字节跳动开发的 AI 人工智能助手\"},\n",
    "        {\"role\": \"user\", \"content\": \"常见的十字花科植物有哪些？\"},\n",
    "    ],\n",
    "    stream=True\n",
    ")\n",
    "for chunk in stream:\n",
    "    if not chunk.choices:\n",
    "        continue\n",
    "    print(chunk.choices[0].delta.content, end=\"\")\n",
    "print()\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-22T05:48:01.586352Z",
     "start_time": "2024-07-22T05:47:42.493678Z"
    }
   },
   "id": "6cfa781a93cb4330",
   "execution_count": 12
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
